package com.lanou3g.musicenjoy.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.LeftAdapter;
import com.lanou3g.musicenjoy.adapter.MoreHelperAdapter;
import com.lanou3g.musicenjoy.custom.MyListView;
import com.lanou3g.musicenjoy.entity.Left;
import com.lanou3g.musicenjoy.entity.More;
import com.lanou3g.musicenjoy.utils.NoScrollListView;
import com.lanou3g.musicenjoy.volley.VolleySingleton;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by dllo on 15/10/24.
 */
public class MoreHelperFragment extends Fragment implements View.OnClickListener {
    private View view;
    private MoreHelperAdapter adapter;
    private List<More.SonglistEntity> datas;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private NoScrollListView leftNestedScrollView;
    private RequestQueue queue;
    private ImageView newLeftImageView;
    private ImageView backImageView;
    private FragmentManager manager;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_left,null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        backImageView= (ImageView) view.findViewById(R.id.backImageView);
        backImageView.setOnClickListener(this);
        newLeftImageView= (ImageView) view.findViewById(R.id.newLeftImageView);
        queue= VolleySingleton.getInstance(getActivity()).getRequestQueue();
        Toolbar toolbar= (Toolbar) view.findViewById(R.id.coordinator_toolbar);
        manager = getFragmentManager();


        collapsingToolbarLayout= (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
        collapsingToolbarLayout.setTitle("乐享音乐");

        collapsingToolbarLayout.setExpandedTitleColor(0x00ffffff);
        collapsingToolbarLayout.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);

        leftNestedScrollView= (NoScrollListView) view.findViewById(R.id.leftNestedScrollView);
        adapter=new MoreHelperAdapter(getActivity());
        leftNestedScrollView.setAdapter(adapter);
        Bundle bundle=getArguments();
        String id=bundle.getString("id");
        String imp=bundle.getString("url");
        Log.i("gjaslj",imp);
        Picasso.with(getActivity()).load(imp).into(newLeftImageView);
        Log.i("FJKLA",id);
        String url="http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.0&method=baidu.ting.album.getAlbumInfo&format=json&album_id="+id;
        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response!=null){
                    Gson gson=new Gson();
                    More result=gson.fromJson(response,More.class);
                    if (result!=null){
                        datas=result.getSonglist();
                        adapter.initData(datas);
                    }
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }


    @Override
    public void onClick(View v) {
        Log.i("xinxin", "点击了back");
        manager.popBackStackImmediate();
    }


}

